﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcEngine.Core.Validation
{
    /// <summary>
    ///  Validates the length of the string value.
    /// </summary>
    public class LengthValidatorAttribute : FieldValidatorAttribute
    {
        public LengthValidatorAttribute(string message)
            : base(message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LengthValidatorAttribute"/> class.
        /// </summary>
        /// <param name="maxLength">The min length.</param>
        public LengthValidatorAttribute(string message, int maxLength)
            : base(message)
        {
            this.MaxLength = maxLength;
            this.MinLength = 0;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LengthValidatorAttribute"/> class.
        /// </summary>
        /// <param name="minLength">The min length.</param>
        /// <param name="maxLength">The max length.</param>
        public LengthValidatorAttribute(string message, int minLength, int maxLength)
            : base(message)
        {
            this.MinLength = minLength;
            this.MaxLength = maxLength;
        }

        /// <summary>
        /// Gets or sets the max length.
        /// </summary>
        /// <value>The max length.</value>
        public int MaxLength
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the min length.
        /// </summary>
        /// <value>The min length.</value>
        public int MinLength
        {
            get;
            set;
        }

        public bool AllowEmpty { get; set; }

        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value to validate.</param>
        /// <returns>
        /// 	<c>true</c> if the specified value is valid; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsValid(object value)
        {
            if (AllowEmpty && value == null)
                return true;

            this.ErrorMessage = string.Format(Message, MinLength.ToString(), MaxLength.ToString());

            if (value == null)
            {
                return true;
            }

            string val = value as string;

            if (val != null)
            {
                if (string.IsNullOrEmpty(val) && AllowEmpty)
                    return true;

                if (MaxLength != 0)
                {
                    return val.Length <= MaxLength && val.Length >= MinLength;
                }

                return val.Length >= MinLength;
            }
            else
            {
                throw new InvalidOperationException("RequiredFieldValidator can be applied only to string properties");
            }
        }
    }
}
